package com.backstage.controller;

import com.backstage.pojo.Goods;
import com.backstage.service.GoodsService;
import com.backstage.utils.JsonResult;
import com.backstage.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping(value = "/admin")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/goods_list", method = RequestMethod.GET)
    public String goodslist(@RequestParam(required = false, defaultValue = "1") int page,
                            @RequestParam(required = false, defaultValue = "10") int pageSize,
                            @RequestParam(required = false, defaultValue = " ") String keyWord,
                            HttpServletRequest request,
                            Model model) {

        PageBean<Goods> pageBean = goodsService.selectGoodsList(page, pageSize, keyWord);
        model.addAttribute("list",pageBean.getList());
        model.addAttribute("pager",pageBean.getPageBeanObj());
        return "admin/goods_list";

    }

    @RequestMapping(value = "/goods_list_json", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult goodslistjson(@RequestParam(required = false, defaultValue = "1") int page,
                                    @RequestParam(required = false, defaultValue = "10") int pageSize,
                                    @RequestParam(required = false, defaultValue = " ") String keyWord){

        JsonResult jsonResult = new JsonResult();
        jsonResult.setSuccess(false);

        try {
            PageBean<Goods> pageBean = goodsService.selectGoodsList(page, pageSize, keyWord);
            jsonResult.setSuccess(true);
            jsonResult.setCode("200");
            jsonResult.setMessage("获取信息成功！");
            jsonResult.setData(pageBean.getList());
            jsonResult.setPager(pageBean.getPageBeanObj());
            return jsonResult;

        } catch (Exception e){
            jsonResult.setCode("systemException");
            jsonResult.setMessage("获取信息失败！");
            return jsonResult;
        }


    }


}
